import { cancelOrder, selOrderList } from '@/api/order'

const state = {
  orderList: []
}
const mutations = {
  setOrderList (state, newOrderList) {
    state.orderList = newOrderList
  }
}

const getters = {
  list (state) {
    return state.orderList
  }
}
const actions = {
  async getOrderList (context, dataType) {
    const { data: { list } } = await selOrderList(dataType)
    console.log(list)
    list.data.forEach(item => { item.total_num = (item.goods.reduce((sum, item) => sum + item.total_num, 0)) })
    context.commit('setOrderList', list.data)
  },
  async cancelOrder (context, orderId) {
    const res = await cancelOrder(orderId)
    console.log(res.data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
